package com.lingshi.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping_common.entity.Admin;
import com.lingshi.shopping_common.entity.Permission;
import com.lingshi.shopping_common.entity.Role;
import com.lingshi.shopping_common.result.BaseResult;
import com.lingshi.shopping_common.service.IRoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台角色
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @DubboReference
    private IRoleService roleService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Role role) {
        roleService.add(role);
        return BaseResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Role role) {
        roleService.update(role);
        return BaseResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long rid) {
        roleService.delete(rid);
        return BaseResult.success();
    }

    /**
     * 根据id查询角色
     */
    @GetMapping("/findById")
    public BaseResult findById(Long rid) {
        Role role = roleService.findById(rid);
        return BaseResult.success(role);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     */
    @GetMapping("/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<Role> search = roleService.search(page, size);
        return BaseResult.success(search);
    }

    /**
     * 修改角色的权限
     *
     * @param rid  角色id
     * @param pids 权限id
     * @return 执行结果
     */
    @PutMapping("/updatePermissionToRole")
    public BaseResult updatePermissionToRole(Long rid, Long[] pids) {
        roleService.updatePermissionToRole(rid,pids);
        return BaseResult.success();
    }

    @GetMapping("/findAll")
    public BaseResult findAll(){
        List<Role> roles = roleService.findAll();
        return BaseResult.success(roles);
    }
}
